package com.atguigu.lease.web.admin.controller.system;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * Package: com.atguigu.lease.web.admin.controller.system
 *
 * @Description:
 * @Author xiongkunYang
 * @Create 2025/4/19 23:06
 */
@Tag(name = "后台用户信息管理")
@RestController
@RequestMapping("/admin/system/user")
@RequiredArgsConstructor
public class SystemUserController {

    private final SystemUserService systemUserService;

    @Operation(summary = "根据条件分页查询后台用户列表")
    @GetMapping("page")
    public Result<IPage<SystemUserItemVo>> page(@RequestParam long current, @RequestParam long size, SystemUserQueryVo queryVo) {
        return Result.ok(this.systemUserService.itemVoPageByQuery(current, size, queryVo));
    }

    @Operation(summary = "根据ID查询后台用户信息")
    @GetMapping("getById")
    public Result<SystemUserItemVo> getById(@RequestParam Long id) {
        return Result.ok(this.systemUserService.getItemVoById(id));
    }

    @Operation(summary = "保存或更新后台用户信息")
    @PostMapping("saveOrUpdate")
    public Result<Boolean> saveOrUpdate(@RequestBody SystemUser systemUser) {
        return Result.ok(this.systemUserService.saveOrUpdate(systemUser));
    }

    @Operation(summary = "判断后台用户名是否可用")
    @GetMapping("isUserNameAvailable")
    public Result<Boolean> isUsernameExists(@RequestParam String username) {
        return Result.ok(!this.systemUserService.exists(new LambdaQueryWrapper<SystemUser>()
                .eq(SystemUser::getUsername, username)));
    }

    @DeleteMapping("deleteById")
    @Operation(summary = "根据ID删除后台用户信息")
    public Result<Boolean> removeById(@RequestParam Long id) {
        return Result.ok(this.systemUserService.removeById(id));
    }

    @Operation(summary = "根据ID修改后台用户状态")
    @PostMapping("updateStatusByUserId")
    public Result<Boolean> updateStatusByUserId(@RequestParam Long id, @RequestParam BaseStatus status) {
        return Result.ok(this.systemUserService.update(new LambdaUpdateWrapper<SystemUser>()
                .eq(SystemUser::getId, id)
                .set(SystemUser::getStatus, status)));
    }
}
